package com.zx.mes.rabbitmq.api.hyl.api.returnlistener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * direct consumer
 *
 * @author huayunlong
 * @date 2018-9-4
 */
public class Conusmer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("admin");
        factory.setPassword("123456");
        factory.setHost("10.30.90.136");
        factory.setPort(5672);
        factory.setVirtualHost("/");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        QueueingConsumer consumer = new QueueingConsumer(channel);

        channel.queueDeclare("test.topic.queue",true,false,false,null);
        channel.exchangeDeclare("test.topic", "topic");


        channel.queueBind("test.topic.queue", "test.topic","test.#");

        channel.basicConsume("test.topic.queue",true, consumer);
        while (true) {
            System.out.println("");
            System.out.println(new String(consumer.nextDelivery().getBody()));
        }

    }
}
